package com.zmc.order.infrastructure.updater.order;

import com.zmc.common.domain.freight.FreightCreateOrder;
import com.zmc.common.domain.freight.FreightSendAddress;
import com.zmc.common.domain.resp.freight.FreightCreateOrderFeignResp;
import com.zmc.common.domain.vo.member.MemberAddressInfo;
import com.zmc.common.exception.base.BusinessException;
import com.zmc.common.utils.JsonUtils;
import com.zmc.order.feign.AdminFeignService;
import com.zmc.order.feign.MemberFeignService;
import com.zmc.order.feign.ThirdFeignService;
import com.zmc.order.infrastructure.entity.MerchantOrderEntity;
import com.zmc.order.infrastructure.entity.OrderEntity;
import com.zmc.order.infrastructure.entity.OrderItemEntity;
import com.zmc.order.infrastructure.enums.OrderEnum;
import com.zmc.order.infrastructure.service.MerchantOrderService;
import com.zmc.order.infrastructure.service.OrderItemService;
import com.zmc.order.infrastructure.service.OrderService;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：zmc
 * @Date：2025/5/15 18:18
 * 订单创建物流单
 */
@Component
public class OrderCreateLogisticsUpdater<T> implements OderUpdater<T> {

    public static Logger log = LoggerFactory.getLogger(OrderCreateLogisticsUpdater.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private MerchantOrderService merchantOrderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private AdminFeignService adminFeignService;

    @Autowired
    private ThirdFeignService thirdFeignService;

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    @Transactional
    @ShardingSphereTransactionType(TransactionType.BASE)
    public void execute(T param) {
        //商户订单号
        String orderSn = (String) param;
        MerchantOrderEntity merchantOrder = merchantOrderService.getByOrderSn(orderSn);
        if (merchantOrder == null) {
            log.error("订单发货-商户订单号不存在, orderSn:{}", orderSn);
            throw new BusinessException("商户订单号不存在");
        }
        if (merchantOrder.getStatus() != OrderEnum.ORDER_STATUS_WAIT_SEND_GOOD.getCode()) {
            log.error("订单发货-当前订单状态无法发货, orderSn:{}", orderSn);
            throw new BusinessException("当前订单状态无法发货");
        }
        OrderEntity order = orderService.getByOrderSn(merchantOrder.getUserOrderSn());
        //创建物流单
        FreightCreateOrder freightCreateOrder = new FreightCreateOrder();
        freightCreateOrder.setOrderId(order.getOrderSn());
        List<OrderItemEntity> list = orderItemService.listByOrderSn(order.getOrderSn());
        freightCreateOrder.setGoodName(list.get(0).getSpuName());
        long sum = list.stream().mapToLong(OrderItemEntity::getSkuQuantity).sum();
        freightCreateOrder.setGoodCount((int) sum);
        //发货人
        FreightSendAddress sendAddress = JsonUtils.transToClass(adminFeignService.getUserIdentity(order.getMerchantId()), FreightSendAddress.class);
        freightCreateOrder.setSendPhone(sendAddress.getPhone());
        freightCreateOrder.setSendPostCode(sendAddress.getPostCode());
        freightCreateOrder.setSendDetailAddress(sendAddress.getProvince() + sendAddress.getCity() + sendAddress.getRegion() + sendAddress.getAddress());
        //接收人
        MemberAddressInfo addressInfo = JsonUtils.transToClass(memberFeignService.receiveAddressInfo(order.getReceiverAddressId()), MemberAddressInfo.class);
        freightCreateOrder.setAcceptPhone(addressInfo.getPhone());
        freightCreateOrder.setAcceptPostCode(addressInfo.getPostCode());
        freightCreateOrder.setAcceptDetailAddress(addressInfo.getProvince() + addressInfo.getCity() + addressInfo.getRegion() + addressInfo.getDetailAddress());
        FreightCreateOrderFeignResp resp = JsonUtils.transToClass(thirdFeignService.createOrder(freightCreateOrder), FreightCreateOrderFeignResp.class);
        //发货单创建成功
        if (resp.isSuccess()) {
            //更新商户订单
            merchantOrderService.updateMerchantOrderWhenSendGood(orderSn, resp.getWmsOrderId(), OrderEnum.ORDER_STATUS_WAIT_GET_GOOD.getCode());
            //更新用户订单
            orderService.updateOrderWhenSendGood(order.getOrderSn(), OrderEnum.ORDER_STATUS_WAIT_GET_GOOD.getCode());
        }
    }
}
